package view.backend;

import exceptionManager.ExceptionManager;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.ProductFamily;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminProductFamily
{
    private JPanel 
            productFamilyPanel,
            scrollPanel;
    private model.ProductFamily prodObjectShown;
    private JTextField tFieldName;
    private JButton btnSend;

    public AdminProductFamily()
    {
        prodObjectShown = new ProductFamily();
        
        productFamilyPanel = new JPanel(null);
        productFamilyPanel.setBounds(395, 320, 650, 500);
        productFamilyPanel.setOpaque(false);
        
        buildLeftSide();
        buildRightSide();
        
        productFamilyPanel.setVisible(false);
    }
    
    private void buildLeftSide()
    {
        ActionListener actionListener =
        (
            new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    addAlter();
                }
            }
        );
        
        JPanel shownProductFamilyPanel = new JPanel(null);
        shownProductFamilyPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Varegruppe", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        shownProductFamilyPanel.setBounds(10, 0, 280, 125);
        shownProductFamilyPanel.setOpaque(false);
        
        JLabel lblName = new JLabel("Navn");
        lblName.setBounds(10, 25, 150, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldName = new JTextField();
        tFieldName.setBounds(10, 45, 260, 25);
        tFieldName.setBorder(DesignLayout.T_FIELD_RED);
        tFieldName.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldName.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldName.setFont(DesignLayout.STANDARD);
        tFieldName.addActionListener(actionListener);
        tFieldName.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                prodObjectShown.setName(tFieldName.getText());
            }
        });
        
        btnSend = new JButton("tilføj");
        btnSend.setBounds(10, 80, 100, 35);
        btnSend.requestFocus(true);
        btnSend.setBorder(DesignLayout.T_FIELD_RED);
        btnSend.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnSend.addActionListener(actionListener);
        
        JButton btnClear = new JButton("reset");
        btnClear.setBounds(200, 80, 70, 35);
        btnClear.setBorder(DesignLayout.T_FIELD_RED);
        btnClear.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnClear.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                clear();
            }
        });
        
        shownProductFamilyPanel.add(lblName);
        shownProductFamilyPanel.add(tFieldName);
        shownProductFamilyPanel.add(btnSend);
        shownProductFamilyPanel.add(btnClear);
        
        productFamilyPanel.add(shownProductFamilyPanel);        
    }
    
    private void addAlter()
    {
        if (tFieldName.getText().length() > 0)
        {
            String qText = "";
            if (prodObjectShown.getId() > 0)
            {
                qText = "Ønsker du at redigere varegruppen?";
            }
            else
            {
                qText = "Ønsker du oprette Varegruppen? Handlingen kan ikke annuleres.";
            }

            int n = JOptionPane.showConfirmDialog(
            null,
            qText,
            "Bekræft Venligst",
            JOptionPane.YES_NO_OPTION);
            if (n == 0)
            {
                try
                {
                    // Brugeren har bekræftet, at varegruppen skal tilføjes
                    int added = control.ProductFamilyControl.addAlterProductFamily(prodObjectShown);
                    buildExistingScrollPanel();
                } 
                catch (SQLException ex)
                {
                    ExceptionManager.handleSQLException(ex);
                } 
                catch (ClassNotFoundException ex)
                {
                    ExceptionManager.handleClassNotFoundException(ex);
                }
            }                    
        }        
    }
    
    private void buildRightSide()
    {
        JPanel existingProductFamilyPanel = new JPanel(null);
        existingProductFamilyPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Eksisterende Varegrupper", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        existingProductFamilyPanel.setBounds(310, 0, 340, 500);
        existingProductFamilyPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        buildExistingScrollPanel();
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 300, 450);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        existingProductFamilyPanel.add(scrollPane);
        productFamilyPanel.add(existingProductFamilyPanel);
    }
    
    public void buildExistingScrollPanel()
    {
        scrollPanel.removeAll();
        
        try
        {
            ProductFamily[] families = model.handlers.ProductFamilyHandler.getProductFamilyArray();
            
            for (int i = 0; i < families.length; i++)
            {
                AdminProductFamilyPanel panel = new AdminProductFamilyPanel(this, families[i]);
                panel.setBounds(10, 10 + (i * 55), 260, 50);
                scrollPanel.setPreferredSize(new Dimension(270, 70 + (i * 55)));
                scrollPanel.add(panel);
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }
    
    private void updateFieldsFromModel()
    {
        tFieldName.setText(prodObjectShown.getName());
        btnSend.setText("opdater gruppe: "+ prodObjectShown.getId());
        btnSend.setBounds(10, 80, 150, 35);
    }
    
    private void clear()
    {
        prodObjectShown.setId(0);
        prodObjectShown.setName("");
        
        tFieldName.setText("");
        btnSend.setText("tilføj");
        btnSend.setBounds(10, 80, 100, 35);        
    }
    
    public void setProductFamilyObject(ProductFamily newObject)
    {
        prodObjectShown = newObject;
        updateFieldsFromModel();
    }

    public JPanel getPanel()
    {
        return productFamilyPanel;
    } 
}